package com.junlong0716.enjoylearning.service

import android.app.IntentService
import android.content.Intent
import android.database.Observable
import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.net.api.NetApi
import com.junlong0716.enjoylearning.utils.RxCountDownUtils
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.orhanobut.logger.Logger
import org.json.JSONObject

/**
 *@author: 巴黎没有摩天轮Li
 *@description: 退出记录时间service
 *@date: Created in 下午1:17 2018/7/29
 *@modified by:
 */
class RecordStudyEndService : IntentService("RecordStudyEndService") {
    override fun onHandleIntent(intent: Intent?) {
        val knowledgeId = intent!!.getIntExtra("KNOWLEDGE_ID", 0)
        val map = HashMap<String, Int>()
        map["userId"] = MyApp.instance().userAccount!!.id
        map["knowledgeId"] = knowledgeId
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_STUDY_EXIT)
                .tag(this)
                .upJson(JSONObject(map))
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObject = JSONObject(response!!.body())
                        val code = jsonObject.getString("code")
                        if (code == "200") {
                            Logger.d("----------退出记录时间成功----------")
                        } else {
                            Logger.d("----------退出记录时间失败----------")
                        }
                    }

                    override fun onError(response: Response<String>?) {
                        super.onError(response)
                        Logger.d("----------onError退出记录时间失败----------")
                    }
                })
    }
}